package com.jelly.web.modules.basics.dictionary.service.impl;

import com.jelly.common.util.StringUtils;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.basics.dictionary.dao.DictionaryMapper;
import com.jelly.web.modules.basics.dictionary.domain.Dictionary;
import com.jelly.web.modules.basics.dictionary.domain.DictionaryExample;
import com.jelly.web.modules.basics.dictionary.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-08-17 10:40
 * @功能描述: 字典接口实现类
 **/
@Service
public class DictionaryServiceImpl implements IDictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    /**
     * 通过字典类型ID获取该类型下的所有字典信息
     * @param typeId
     * @return
     */
    @Override
    public List<Dictionary> getDictionaryTypeId(String typeId) {

        DictionaryExample example = new DictionaryExample();

        example.createCriteria().andTypeIdEqualTo(typeId);

        return dictionaryMapper.selectByExample(example);

    }
    /**
     * 主键查询字典详情
     * @param id
     * @return
     */
    @Override
    public Dictionary getDictionaryId(String id) {
        return dictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean addDictionary(Dictionary dictionary) {
        dictionary.setDictionaryId(UUID.randomUUID().toString().replaceAll("-", ""));
        dictionary.setAddId(TokenUtil.getUserId());
        dictionary.setAddDate(new Date());
        return dictionaryMapper.insertSelective(dictionary) > 0;
    }

    @Override
    public boolean updateDictionary(Dictionary dictionary) {
        if(StringUtils.isBlank(dictionary.getDictionaryId())){
            dictionary.setDictionaryId(dictionary.getBaseId());
        }
        dictionary.setUpdateDate(new Date());
        dictionary.setUpdateId(TokenUtil.getUserId());
        return dictionaryMapper.updateByPrimaryKeySelective(dictionary)>0;
    }

    @Override
    public boolean delectDictionary(String id) {
        return dictionaryMapper.deleteByPrimaryKey(id)>0;
    }
}
